<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Create elements for input
 *
 * @author Nugroho
 */
class Application_Form_Elements {
    public function hiddenId(){
        $id = new Zend_Form_Element_Hidden("admin_id");
        $id->addFilter("Int");
        return $id;
    }
    /*
    public function formUsername($label = "Username", $len = 30, $name = "username"){
        $username = new Zend_Form_Element_Text($name);
        $username->setLabel($label)
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $username;
    }
    */
    public function formPassword($label = "Password", $len = 20, $name = "password"){
        $password = new Zend_Form_Element_Password($name);
        $password->setLabel("$label (maks. $len)")
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $password;
    }

    public function formName($label = "Full Name", $len = 35, $name = "full_name"){
        $fullname = new Zend_Form_Element_Text($name);
        $fullname->setLabel($label)
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $fullname;
    }

    public function formEmail($label = "Email", $len = 30, $name = "email"){
        $email = new Zend_Form_Element_Text($name);
        $email->setLabel($label)
             ->setRequired("true")
             ->addFilter("StripTags")
             ->addFilter("StringTrim")
             ->addValidators(array('EmailAddress'=>'EmailAddress'))
             ->addErrorMessages(array('EmailAddress'=>"Please insert ".strtolower($label)." correctly."))
             ->setAttrib("maxLength",$len);
        return $email;
    }

    public function formContact($label = "Contact No.", $len = 18, $name = "contact"){
        $contact = new Zend_Form_Element_Text($name);
        $contact->setLabel($label)
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("regex",true,array('pattern' => "/^0([0-9]{1,3}-)?[0-9]{6,}$/"))
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $contact;
    }

    public function formRole($label = "Role", $name = "role_id"){
        //First, retrive all existing roles from DB except superadmin
        $roles_db = new Backend_Model_DbTable_Role();
        $roles = $roles_db->fetchAll("role_id > 1");
        $rolesArr = array();
        foreach($roles as $role){
            $rolesArr[$role->role_id] = $role->name;
        }

        //Next, insert roles into select element
        $select_role = new Zend_Form_Element_Select($name);
        $select_role->setRequired(true)
                 ->setLabel($label)
                 ->addMultiOptions($rolesArr);
        return $select_role;
    }

    public function formSupervisor($label = "Supervisor", $name = "supervisor_id"){
        //First, retrive all existing sales
        $admin_db = new Backend_Model_DbTable_Admin();
        $sales = $admin_db->getAllSales();
        $salesArr = array("0" => "None");
        foreach($sales as $val){
            $salesArr[$val->admin_id] = $val->full_name.' ('.$val->email.')' ;
        }

        //Next, insert sales into select element
        $select_role = new Zend_Form_Element_Select($name);
        $select_role->setRequired(true)
                 ->setLabel($label)
                 ->addMultiOptions($salesArr);
        return $select_role;
    }

    public function formIcNumber($label = "IC Number", $len = 16, $name = "ic_number"){
        $ic = new Zend_Form_Element_Text($name);
        $ic->setLabel($label)
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $ic;
    }

    public function formCompany($label = "Company", $len = 20, $name = "company"){
        $contact = new Zend_Form_Element_Text($name);
        $contact->setLabel($label)
                ->setRequired("true")
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addValidator("NotEmpty")
                ->setAttrib("maxLength", $len)
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        return $contact;
    }

    public function formGender($label = "Gender", $name = "gender"){
        $gender = array('M' => 'Male', 'F' => 'Female');
        $select_gender = new Zend_Form_Element_Select($name);
        $select_gender->setRequired(true)
                 ->setLabel($label)
                 ->addMultiOptions($gender);
        return $select_gender;
    }

    public function formNumber($label, $name, $len = null){
        $number = new Zend_Form_Element_Text($name);
        $number->setLabel($label)
                ->setRequired()
                ->addFilter("StripTags")
                ->addFilter("StringTrim")
                ->addFilter("Int")
                ->addValidator(new Zend_Validate_Digits())
                ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        if(!is_null($len)) $number->setAttrib("maxLength", $len);
        return $number;
    }

    public function formDate($label = 'Date', $name = 'date', $readonly = true){
        $date = new Zend_Form_Element_Text($name);
        $date->setLabel($label)
             ->setRequired()
             ->addErrorMessages(array("NotEmpty" => "Please insert ".strtolower($label)." correctly."));
        if($readonly) $date->setAttrib('readonly', 'readonly');
        return $date;
    }

    public function formFile($destination, $size, $ext, $label = "File", $name = "file"){
        $file = new Zend_Form_Element_File($name);
        $file->setLabel($label)
                 ->setDestination($destination)
                 ->addValidator('Count', false, 1)
                 ->addValidator('Size', false, $size)
                 ->addValidator('Extension', false, $ext)
                 ->addErrorMessage(array("error"
                        =>"Please upload ".  strtolower($label)." with
                            extension ".$ext." and
                            max. size ".$size." bytes"));
        return $file;
    }

    public function formSubmit($label = "Submit", $name = "submit"){
        $submit = new Zend_Form_Element_Submit($name);
        $submit->setLabel($label);
        return $submit;
    }

    public function formReset($label = "Reset", $name = "reset"){
        $reset = new Zend_Form_Element_Reset($name);
        $reset->setLabel($label);
        return $reset;
    }
}
?>
